package com.plan.dao.service.card.impl;

import com.plan.controller.card.response.AwardProductListRes;
import com.plan.dao.mapper.card.AwardProductMapper;
import com.plan.dao.mapper.card.CardMapper;
import com.plan.dao.mapper.card.PointLogMapper;
import com.plan.dao.mapper.card.PrizeDrawLogMapper;
import com.plan.dao.service.card.AwardDaoService;
import com.plan.entity.card.AwardProduct;
import com.plan.entity.card.Card;
import com.plan.entity.card.PointLog;
import com.plan.entity.card.PrizeDrawLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AwardDaoServiceImpl implements AwardDaoService {
    @Resource
    private AwardProductMapper awardProductMapper;
    @Resource
    private CardMapper cardMapper;
    @Resource
    private PrizeDrawLogMapper prizeDrawLogMapper;
    @Resource
    private PointLogMapper pointLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(AwardProduct product) {
        return awardProductMapper.insert(product);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AwardProduct product) {
        awardProductMapper.update(product);
    }

    @Override
    public AwardProduct selectOneById(Long teamId, Long productId) {
        return awardProductMapper.selectOneById(teamId, productId);
    }

    @Override
    public Integer getExistRateByTeamId(Long teamId) {
        return awardProductMapper.getExistRateByTeamId(teamId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long teamId, Long productId) {
        return awardProductMapper.delete(teamId, productId);
    }

    @Override
    public List<AwardProductListRes> getAwardProductList(Long teamId) {
        return awardProductMapper.getAwardProductList(teamId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePrizeDrawResult(Card card, PrizeDrawLog prizeDrawLog, PointLog pointLog) {
        cardMapper.update(card);
        prizeDrawLogMapper.insert(prizeDrawLog);
        pointLogMapper.insert(pointLog);
    }

}
